#!/bin/bash

cd mct/monitor/mct-public/lib
	  
PLATFORM=`uname -i`
if [ "x$PLATFORM" = "x" -o "x$PLATFORM" = "xunknown" ]; then
	PLATFORM="i386"
fi

l_os_type=`cat /etc/redhat-release | awk '{print $1}' | tr A-Z a-z `;
if [ $l_os_type = "red" ]; then
    l_os_ver=`cat /etc/redhat-release | awk '{print $5}'`;
    if [ $l_os_ver = "8.0" ]; then
        l_os_ver="8.0";
			  l_os_type="redhat";
	  elif [ $l_os_ver = "Server" ]; then
		    l_os_ver=`cat /etc/redhat-release | awk '{print $7}'`;
			  l_os_type="rhel";
    else
        l_os_ver="7.2";
			  l_os_type="redhat";
    fi;
else
    l_os_ver=`cat /etc/redhat-release | awk '{print $3}'`;
    l_os_type="centos"
fi;

rm -rf *.so.*

if [ $PLATFORM == 'x86_64' ]; then
  if [[ "$l_os_ver" > "6.0" ]]; then
    cp -f centos63-64/*.so* ./
  else
    cp -f centos53-64/*.so* ./  
  fi
else
  if [[ "$l_os_ver" > "6.0" ]]; then
    cp -f centos63-32/*.so* ./
  else
    cp -f centos53-32/*.so* ./  
  fi
fi

ln -sf libACE.so.6.1.7 libACE.so.6
ln -sf libACE.so.6   libACE.so
ln -sf liblog4cpp.so.5.0.6 liblog4cpp.so.5
ln -sf liblog4cpp.so.5  liblog4cpp.so

# build mct_os lib
echo "-----------------libmct_os build start----------------------"
cd ../src/mct_os
make clean
make
if [ $? -eq 0 ]; then
	echo "-----------------libmct_os build end [success]----------------------"
else
	echo "-----------------libmct_os build end [failure]----------------------"
	exit $?
fi

# build mct_public lib
echo "-----------------libmct_public build start----------------------"
cd ../mct_commlib
make clean
make
if [ $? -eq 0 ]; then
	echo "-----------------libmct_public build end [success]----------------------"
else
	echo "-----------------libmct_public build end [failure]----------------------"
	exit $?
fi

cd ../../lib

unlink libACE.so
unlink libACE.so.6
unlink liblog4cpp.so
unlink liblog4cpp.so.5

tar -zcvf WBXmctos-${mct_os_version}-${BUILD_NUMBER}-${l_os_type}${l_os_ver}.tar.gz *.so* ../include/
exit 0